package cn.cds.withyou.admin.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ScrollView;

import com.squareup.otto.Subscribe;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.cds.withyou.admin.Application;
import cn.cds.withyou.admin.R;
import cn.cds.withyou.admin.actions.ActionsCreator;
import cn.cds.withyou.admin.stores.CourseStore;
import cn.cds.withyou.admin.ui.widget.CommonTitleBar;
import cn.cds.withyou.lib.model.CourseDto;
import cn.ljs.android.buses.ActionBus;
import cn.ljs.android.buses.DataBus;
import cn.ljs.android.ui.BaseActivity;
import cn.ljs.android.util.AppUtils;
import cn.ljs.android.util.TUtil;

/**
 * <p> Description: </p>
 * <p/>
 * <p> Copyright: Copyright (c) 2016 </p>
 *
 * @author: Json.Lee
 * @Version: 1.0
 * @CreateTime: 8/5/2016 00:39
 */
public class CourseAddedActivity extends BaseActivity {

    @Bind(R.id.title_et)
    EditText mTitleEt;
    @Bind(R.id.content_et)
    EditText mContentEt;
    @Bind(R.id.course_pic_et)
    EditText mPicEt;
    @Bind(R.id.course_et)
    EditText mCourseEt;
    @Bind(R.id.content_scroll)
    ScrollView mContentScroll;

    private ActionsCreator mCreator;
    private ActionBus mActionBus;
    private DataBus mDataBus;
    private CourseStore mStore;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_add_course);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        mContentScroll.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                AppUtils.hideKeyBoard(CourseAddedActivity.this);
                return false;
            }
        });

        mCreator = ActionsCreator.get();
        mActionBus = Application.getInstance().getActionsBus();
        mDataBus = Application.getInstance().getDataBus();
        mStore = CourseStore.get();

        mDataBus.register(this);
        mActionBus.register(mStore);
    }

    @Override
    protected void onDestroy() {
        mActionBus.unregister(mStore);
        mDataBus.unregister(this);
        super.onDestroy();
    }

    @OnClick({R.id.add_btn})
    public void onClick(View v) {
        switch (v.getId()) {
            case CommonTitleBar.LEFT_BUTTON_ID:
                finish();
                break;
            case R.id.add_btn:
                AppUtils.hideKeyBoard(this);
                CourseDto course = getParams();
                if (checkParams(course)) {
                    mCreator.requestCourseAdded(course);
                }
                break;
            default:
                return;
        }
    }

    @Subscribe
    public void updateUI(CourseStore.CourseEvent event) {
        switch (event.code) {
            case CourseStore.CourseEvent.ADD_COURSE_SUCC:
                TUtil.shortToast("添加成功。");
                finish();
                break;
            case CourseStore.CourseEvent.ADD_COURSE_FAIL:
                TUtil.shortToast("添加失败，请重试。");
                break;
            default:
                return;
        }
    }

    private boolean checkParams(CourseDto course) {
        if (TextUtils.isEmpty(course.getTitle())) {
            TUtil.shortToast("请输入教程标题。");
            return false;
        }
        if (TextUtils.isEmpty(course.getContent())) {
            TUtil.shortToast("请输入教程简介。");
            return false;
        }
        if (TextUtils.isEmpty(course.getPic())) {
            TUtil.shortToast("请输入教程简图片链接。");
            return false;
        }
        if (TextUtils.isEmpty(course.getUrl())) {
            TUtil.shortToast("请输入教程链接。");
            return false;
        }
        return true;
    }

    private CourseDto getParams() {
        CourseDto course = new CourseDto();
        course.setTitle(mTitleEt.getText().toString().trim());
        course.setContent(mContentEt.getText().toString().trim());
        course.setCreateTime(System.currentTimeMillis());
        course.setPic(mPicEt.getText().toString().trim());
        course.setUrl(mCourseEt.getText().toString().toString());
        return course;
    }
}
